package com.newly.driver.sdk.device.video.cloud.device;

import cn.hutool.json.JSONObject;
import com.newly.driver.sdk.device.video.constants.DeviceConstant;
import com.newly.driver.sdk.device.video.service.impl.qiniu.AbstractQiNiuCloud;
import com.tencentcloudapi.iotvideoindustry.v20201201.models.AllDeviceInfo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;

/**
 * @author gjm guan
 * @date 2021/10/18 15:52
 * @desc 查询设备列表响应参数
 */
@Data
public class QueryDeviceResp {
    /**
     * 状态码
     */
    @ApiModelProperty("状态码")
    private int code;
    /**
     * 设备总数
     */
    @ApiModelProperty("设备总数")
    private long total;

    /**
     * 数据
     */
    @ApiModelProperty("数据")
    private List<Payload> payloads;

    @Data
    public static class Payload {
        /**
         * 设备id
         */
        @ApiModelProperty("设备id")
        private String deviceId;

        /**
         * 设备名称
         */
        @ApiModelProperty("设备名称")
        private String deviceName;

        /**
         * 国标id
         */
        @ApiModelProperty("国标id")
        private String gbId;

        /**
         * 设备类型
         */
        @ApiModelProperty("设备类型")
        private int deviceType;

        /**
         * 设备在线状态
         * 0：设备不在线；1：设备在线；2：设备隔离中；3：设备未注册
         */
        @ApiModelProperty("设备在线状态")
        private int status;

        /**
         * 创建时间
         */
        @ApiModelProperty("创建时间")
        private int createTime;

        public Payload() {
        }

        // tencent
        public Payload(AllDeviceInfo allDeviceInfo) {
            this.deviceId = allDeviceInfo.getDeviceId();
            this.deviceName = allDeviceInfo.getNickName();
            this.gbId = allDeviceInfo.getDeviceCode();

            // 统一设备类型
            int deviceType = allDeviceInfo.getDeviceType().intValue();
            if (deviceType == 2) {
                this.deviceType = DeviceConstant.DeviceType.IPC.getKey();
            } else if (deviceType == 3) {
                this.deviceType = DeviceConstant.DeviceType.NVR.getKey();
            }

            // 统一设备状态
            if (allDeviceInfo.getStatus().intValue() == 1) {
                this.status = DeviceConstant.DeviceState.ONLINE.getKey();
            } else {
                this.status = DeviceConstant.DeviceState.OFFLINE.getKey();
            }

            this.createTime = allDeviceInfo.getCreateTime().intValue();
        }

        // qiniu
        public Payload(JSONObject jsonObject) {
            this.deviceId = jsonObject.getStr("gbId");
            this.deviceName = jsonObject.getStr("name");
            this.gbId = jsonObject.getStr("gbId");

            // 统一设备类型
            Integer type = jsonObject.getInt("type");
            if (type == 1) {
                this.deviceType = DeviceConstant.DeviceType.IPC.getKey();
            } else if (type == 2) {
                this.deviceType = DeviceConstant.DeviceType.NVR.getKey();
            }

            // 统一设备状态
            AbstractQiNiuCloud.QiniuDeviceState state = AbstractQiNiuCloud.QiniuDeviceState.getInstance(jsonObject.getStr("state"));
            if (state.getKey() == AbstractQiNiuCloud.QiniuDeviceState.ONLINE.getKey()) {
                this.status = DeviceConstant.DeviceState.ONLINE.getKey();
            } else {
                this.status = DeviceConstant.DeviceState.OFFLINE.getKey();
            }
            this.createTime = jsonObject.getInt("createdAt");

        }
    }
}
